/*
 * Copyright (c) 2020 Carnegie Lee
 * All rights reserved
 */
package com.geekc.rabbitmq.producer;

import com.geekc.rabbitmq.domain.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 业务生产者
 * @author Carnegie Lee
 */
@Component
public class Producer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitTemplate.ConfirmCallback confirmCallback;

    @Autowired
    private RabbitTemplate.ReturnCallback returnCallback;

    public void sendMessage(Message message) {
        /** 作为消息的附加信息传递，通常保存消息的全局唯一标识ID */
        CorrelationData correlationData = new CorrelationData(message.getMessageId() + "-" + new Date().getTime());
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        rabbitTemplate.convertAndSend("exchange.demo", "queue.demo", message, correlationData);
    }
}
